package com.hl.sfzy.controller;


import com.hl.sfzy.constant.Result;
import com.hl.sfzy.entity.SysUser;
import com.hl.sfzy.exception.AppException;
import com.hl.sfzy.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author ffc
 * @version 1.0
 * @description: TODO
 * @className: LoginController
 * @date 2024/7/15 9:52
 */

@RestController
public class LoginController {
    @Autowired
    LoginService loginService;

    //登录
    @GetMapping("/login")
    public Result login(String username,String userpwd, Integer source, HttpSession session){
        SysUser sysUser = new SysUser();
        sysUser.setUsername(username);
        sysUser.setUserpwd(userpwd);
        System.out.println("================================================");
        System.out.println(sysUser.getUsername());
        SysUser entity = loginService.login(sysUser, source);
        if(entity!=null){
            session.setAttribute("loginInfo",entity);
            return Result.success(entity);
        }
        throw new AppException("402");
    }
    @GetMapping("/logout")
    public Result logout(HttpSession session){
        session.invalidate();
        return Result.success();
    }
}
